<?php
class Ynblog_Model_Blog extends Core_Model_Item_Abstract {
	/*----- Properties -----*/
	protected $_parent_type = 'user';
	protected $_parent_is_owner = true;
	protected $_searchColumns = array(
		'title',
		'body'
	);
	protected $_type = "blog";

	/*----- Parse Blog Title URL For Friendly URL Display -----*/
	public function getSlug($str = null) {
		$str = $this -> getTitle();
		if (strlen($str) > 32) {
			$str = Engine_String::substr($str, 0, 32) . '...';
		}
		$str = preg_replace('/([a-z])([A-Z])/', '$1 $2', $str);
		$str = strtolower($str);
		$str = preg_replace('/[^a-z0-9-]+/i', '-', $str);
		$str = preg_replace('/-+/', '-', $str);
		$str = trim($str, '-');
		if (!$str) {
			$str = '-';
		}
		return $str;
	}

	/*----- Get Link of Blog Function -----*/
	public function getHref($params = array()) {
		$slug = $this -> getSlug();
		$params = array_merge(array(
			'route' => 'blog_entry_view',
			'reset' => true,
			'user_id' => $this -> owner_id,
			'blog_id' => $this -> blog_id,
			'slug' => $slug
		), $params);
		$route = $params['route'];
		$reset = $params['reset'];
		unset($params['route']);
		unset($params['reset']);
		return Zend_Controller_Front::getInstance() -> getRouter() -> assemble($params, $route, $reset);
	}

	/*----- Get Blog Description Function ----*/
	public function getDescription() {
		$tmp_body = strip_tags($this -> body);
		$description = Engine_Api::_() -> ynblog() -> subPhrase($tmp_body, 255);
		return $description;
	}

	/*----- Get Tag Key Words -----*/
	public function getKeywords($seperator = ' ') {
		$keywords = array();
		// Get tags
		foreach ($this->tags()->getTagMaps() as $tagmap) {
			$tag = $tagmap -> getTag();
			$keywords[] = $tag -> getTitle();
		}

		// Return result
		if (null === $seperator) {
			return $keywords;
		}
		return join($seperator, $keywords);
	}

	/*----- Comment Objects Function -----*/
	public function comments() {
		return new Engine_ProxyObject($this, Engine_Api::_() -> getDbtable('comments', 'core'));
	}

	/*----- Like Objects Function -----*/
	public function likes() {
		return new Engine_ProxyObject($this, Engine_Api::_() -> getDbtable('likes', 'core'));
	}

	/*----- Tag Objects Function -----*/
	public function tags() {
		return new Engine_ProxyObject($this, Engine_Api::_() -> getDbtable('tags', 'core'));
	}

	/*----- Count Number Of Blog's Like  Function -----*/
	public function getCountLikes() {
		$table = Engine_Api::_() -> getDbtable('likes', 'core');
		$Name = $table -> info('name');
		$select = $table -> select() -> from($Name);
		$select -> where("resource_id = ?", $this -> blog_id) -> where("resource_type  LIKE 'blog'");
		$blogs = $table -> fetchAll($select);
		return count($blogs);
	}

	/*----- Get Featured Blogs Function -----*/
	public function getFeatured() {
		$ftable = Engine_Api::_() -> getDbtable('features', 'ynblog');
		$fName = $ftable -> info('name');
		$select = $ftable -> select() -> from($fName) -> where("blog_id = ?", $this -> blog_id);
		$features = $ftable -> fetchAll($select);
		if (count($features) <= 0)
			return false;
		else {
			if ($features[0] -> blog_good == '1')
				return true;
			else
				return false;
		}
		return false;
	}

	/*----- Get Blog Status Function ---*/
	public function getStatus() {
		$status = array();
		if ($this -> draft) {
			$status['type'] = 0;
			$status['condition'] = 'draft';
			return $status;
		}
		if ($this -> is_approved) {
			$status['type'] = 2;
			$status['condition'] = 'approved';
		}
		else {
			$status['type'] = 1;
			$status['condition'] = 'approving';
		}
		return $status;
	}

	/*----- Delete Information Related To Blog Function -----*/
	protected function _delete() {
		if ($this -> _disableHooks)
			return;

		//Delete Blog Member
		$become_table = Engine_Api::_() -> getDbTable('becomes', 'ynblog');
		$become_selected = $become_table -> select() -> where('blog_id = ?', $this -> getIdentity());
		foreach ($become_table->fetchAll($become_selected) as $blog_become) {
			$blog_become -> delete();
		}

		parent::_delete();
	}

	public function checkPermission($blog_id) {
		// Check permission
		$viewer = Engine_Api::_() -> user() -> getViewer();
		$blog = Engine_Api::_() -> getItem('blog', $blog_id);
		if (!Zend_Controller_Action_HelperBroker::getStaticHelper('requireAuth') -> setAuthParams($blog, $viewer, 'view') -> checkRequire()) {
			return false;
		}
		return true;
	}

}
?>